package com.smart.lib.service;

import com.smart.lib.entity.AdminInfo;
import com.smart.lib.entity.SystemInfo;
import com.smart.lib.mapper.AdminMapper;
import com.smart.lib.mapper.BorrowMapper;
import com.smart.lib.util.Response;
import com.smart.lib.util.TokenUtils;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 管理员接口实现类
 * */
@Service
public class AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    BookService bookService;
    @Autowired
    UserService userService;
    @Autowired
    BorrowService borrowService;

    /**
     * 管理员登录方法
     * */
    public Response validateAdmin(AdminInfo adminInfo){
        AdminInfo admin = adminMapper.validateAdmin(adminInfo);
        if(admin!=null){
            //登录成功
            //加入token认证
            String token = TokenUtils.sign(admin);
            HashMap<String,Object> hs = new HashMap<>();
            hs.put("token",token);
            hs.put("user",admin);
            return new Response(200,"登录成功",hs);
        }else {
            return new Response(203,"登录失败",null);
        }
    }

    /**
     * 管理首页获取图书基础信息数据
     * */
    public Response getCurrentLibData(){
        SystemInfo sys = new SystemInfo();
        sys.setBook_total(bookService.getBookTotal());
        sys.setUser_total(userService.getUserTotal());
        sys.setBorrow_today(borrowService.getCurrentBorrowTotal());
        sys.setBorrowing_total(borrowService.getBorrowingTotal());
        return new Response(200,"查询成功",sys);
    }
}
